package com.dylan.shot.head.controller;

import com.dylan.shot.common.model.Member;
import com.dylan.shot.head.service.MemberAuthenticateService;
import com.dylan.shot.head.shiro.ShiroHelper;
import com.dylan.shot.head.vo.SimpleTip;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Dylan on 2017/9/12.
 */
@Controller
public class LoginController {
    private Logger LOG = LoggerFactory.getLogger(getClass());

    @Autowired
    private MemberAuthenticateService memberAuthenticateService;
    @Autowired
    private EhCacheManager ehCacheManager;

    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            return "redirect:/";
        }
        String shiroLoginFailure = (String) request.getAttribute("shiroLoginFailure");
        LOG.info("shiroLoginFailure:{}", shiroLoginFailure);
        if (StringUtils.isNotBlank(shiroLoginFailure)) {
            model.addAttribute("loginMessage", "用户名/密码错误");
        }
        return "login";
    }

    @RequestMapping(value = {"", "/", "index", "home"}, method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    /*
    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            return "redirect:/";
        }
        String shiroLoginFailure = (String) request.getAttribute("shiroLoginFailure");
        LOG.info("shiroLoginFailure:{}", shiroLoginFailure);
        if (StringUtils.isNotBlank(shiroLoginFailure)) {
            model.addAttribute("loginMessage", "用户名/密码错误");
        }
        return "login";
    }

    @RequestMapping(value = "/register")
    @ResponseBody
    public SimpleTip register(@RequestParam String username, @RequestParam String password) {
        Member member = new Member();
        member.setUsername(username);
        member.setPassword(password);
        memberAuthenticateService.createMember(member);
        return SimpleTip.success().putInfo("memberId", member.getMemberId()).bindInfo();
    }

    @RequestMapping(value = "/modify_password")
    @ResponseBody
    public SimpleTip modifyPassword(@RequestParam String password) {
        Member member = ShiroHelper.getCachedMember();
        Member up = new Member();
        up.setMemberId(member.getMemberId());
        up.setPassword(password);
        memberAuthenticateService.updateMemberByMemberId(up);
        return SimpleTip.success();
    }

    @RequiresRoles("admin")
    @RequiresAuthentication
    @RequestMapping(value = "/modify_self_nickname")
    @ResponseBody
    public SimpleTip getMember(@RequestParam String nickname) {
        Member member = ShiroHelper.getCachedMember();
        Member up = new Member();
        up.setMemberId(member.getMemberId());
        up.setNickname(nickname);
        memberAuthenticateService.updateMemberByMemberId(up);
        return SimpleTip.success().putInfo("member", ShiroHelper.getCachedMember()).bindInfo();
    }
    */

    @RequestMapping(value = "/self_info")
    @ResponseBody
    public SimpleTip myInfo() {
        Member member = ShiroHelper.getCachedMember();
        return SimpleTip.success().putInfo("member", member).bindInfo();
    }

    @RequestMapping(value = "/self_info2")
    @ResponseBody
    public SimpleTip myInfo2() {
        Member member = ShiroHelper.getCachedMember();
        return SimpleTip.success().putInfo("member", member).bindInfo();
    }

    @RequestMapping(value = "/cache")
    public String cache(Model model) {
        Cache<String, Object> authorizationCache = ehCacheManager.getCache("authorizationCache");
        Cache<String, Object> authenticationCache = ehCacheManager.getCache("authenticationCache");
        Cache<String, Object> shiroActiveSessionCache = ehCacheManager.getCache("shiro-activeSessionCache");
        model.addAttribute("authorizationCache", authorizationCache);
        model.addAttribute("authenticationCache", authenticationCache);
        model.addAttribute("shiroActiveSessionCache", shiroActiveSessionCache);
        return "cache";
    }
}
